/**
 * 证书管理相关JS
 *
 * Created by Zmt on 2015/11/04.
 */
var BASE_URL = "/cer";
/**
 * 下载证书
 *
 * @param id
 */
function downloadCer(id)
{
    if (0 != id && null != id)
    {
        $("#info_msg").text("您确定要下载吗？");
        $("#info_dialog").dialog({
            dialogClass: "my-dialog",
            autoOpen: true,
            modal: true,
            title: '下载确认',
            buttons: {
                "确定": function () {
                    window.location.href = BASE_URL + "/downloadCer.do?id=" + id;
                    $("#info_dialog").dialog("close");
                },
                取消: function () {
                    $("#info_dialog").dialog("close");
                }
            }
        });
    }
    else
        $("#info_msg").text("您还没有选中任何证书，请选中后再下载！");
}

/**
 * 获取号段下的证书
 *
 * @param sid
 */
function getCertificate(sid)
{
    $.post("/cer/queryCertificate.do?sid=" + sid, function (result) {
        $("#cer_dlg").html(result);
    });
    $("#cer_dlg").dialog({
        dialogClass: "my-dialog",
        autoOpen: true,
        width: 1000,
        modal: true,
        title: "查看证书日志",
        buttons: {
            //"保存": function () {
            //    store(BASE_URL + "/modifyLocalSection.do?sid=" + sid, true)
            //},
            取消: function () {
                $(this).dialog("close");
            }
        }
    });
}

/**
 * 删除证书
 *
 * @param url
 * @param ids
 */
function deleteCer(id)
{
    if (0 != id && null != id)
    {
        $("#info_msg").text("删除后将无法恢复，您确定要删除吗？");
        $("#info_dialog").dialog({
            dialogClass: "my-dialog",
            autoOpen: true,
            modal: true,
            title: '删除确认',
            buttons: {
                "确定": function () {
                    $.post(BASE_URL + "/deleteCer.do?id=" + id, function (result) {
                        if (result.ok)
                        {
                            $("#info_dialog").dialog("close");
                            window.location.reload(false);
                        }
                        else {
                            alert(result.msg);
                        }
                    }, 'json');
                },
                取消: function () {
                    $(this).dialog("close");
                }
            }
        });
    }
    else
        $("#info_msg").text("您还没有选中任何证书，请选中后再删除！");
}

/**
 * 提交申请
 */
function apply()
{
    if (null != sid)
    {
        $("#fm_apply").form("submit", {
            url: "/cer/apply.do?sid=" + sid,
            onSubmit: function () {
                return $(this).form("validate");
            },
            success: function (result) {
                var result = $.parseJSON(result);
                if (result.ok)
                {
                    $.messager.show({title: '成功', msg: "申请成功"});
                    $("#dlg_apply").dialog("close");
                    $('#dg').datagrid('reload');
                }
                else
                    $.messager.alert("提示信息", result.msg);
            }
        });
    }
    else
        $.messager.alert("提示信息", "该号段不存在！");
}

/*类型校验*/
function checkType(){
    var bValid = true;
   var cer_type = document.getElementById("apl_cre_type");
    var data = cer_type.value;

    if(data == 0){
        bValid = false;
        $("#apl_cre_type").addClass("ui-state-error");
        $("#data_cre_tips").text("证书类型为必填项！").addClass("ui-state-highlight");
    }
    if(bValid){
        $("#apl_cre_type").removeClass("ui-state-error");
        $("#data_cre_tips").text("").removeClass("ui-state-highlight");
    }
    return bValid;
}
/**
 * 申请证书
 *
 * @param sid
 * @param sName
 */
function getApply(sid, sName, state)
{
    if (state == 1)
    {
        var dlg = $("#cer_apply_dlg");
        $.post(BASE_URL + "/getCerType.do?sName=" + sName, function (result) {
            dlg.html(result);
        });
        dlg.dialog({
            dialogClass: "my-dialog",
            autoOpen: true,
            width: 850,
            modal: true,
            title: "申请证书",
            buttons: {
                "保存": function () {
                    if(checkType()){
                        store(BASE_URL + "/apply.do?sid=" + sid, dlg, $("#cer_apply_fm"))
                    }
                },
                取消: function () {
                    $(this).dialog("close");
                }
            },
            close:function(){
                $("#apl_cre_type").removeClass("ui-state-error");
                $("#data_cre_tips").text("").removeClass("ui-state-highlight");
            }
        });
        $(":input", "#cer_apply_fm").not(":button, :submit, :reset, :hidden").val("");
    }
    else
    {
        $("#info_msg").css("text-align", "center").text("该码段未启用，无法申请证书！");
        $("#info_dialog").dialog({
            dialogClass: "my-dialog",
            autoOpen: true,
            modal: true,
            title: '提示信息',
            buttons: {
                确定: function () {
                    $(this).dialog("close");
                }
            }
        });
    }
}